package com.ramp.microswing;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;



public class Graphics2D {
	
	public static final int VERTICAL_GRADIENT = 0;
	
	public static final int HORIZONTAL_GRADIENT = 1;
	
	private int[] imageData;
	
	private static Image sombra;
	
	private static Image sombraf;

	
	public Graphics2D(){
		
	}
	
	/** The Constant HEAD_UP. */
	public static final int HEAD_UP = 0;

	/** The Constant HEAD_DOWN. */
	public static final int HEAD_DOWN = 1;

	/** The Constant HEAD_LEFT. */
	public static final int HEAD_LEFT = 2;

	/** The Constant HEAD_RIGHT. */
	public static final int HEAD_RIGHT = 3;
	
	/** The Constant HEAD_DOWN_RECTANGLE_MODE. */
	public static final int HEAD_DOWN_RECTANGLE_MODE = 4;
	
	/** The Constant HEAD_UP_RECTANGLE_MODE. */
	public static final int HEAD_UP_RECTANGLE_MODE = 5;

	/**
	 * Fill triangle.
	 * 
	 * @param g the g
	 * @param x the x
	 * @param y the y
	 * @param w the w
	 * @param h the h
	 * @param orientation the orientation
	 */
	
	public static final Image rotateImage(Image image, int angle) throws Exception
	{
		if(angle == 0)
		{
			return image; 
		}
		else if(angle != 180 && angle != 90 && angle != 270)
		{
			throw new Exception("Invalid angle");
		}
	 
		int width = image.getWidth();
		int height = image.getHeight();
	 
		int[] rowData = new int[width];
		int[] rotatedData = new int[width * height];
	 
		int rotatedIndex = 0;
	 
		for(int i = 0; i < height; i++)
		{
			image.getRGB(rowData, 0, width, 0, i, width, 1);
	 
			for(int j = 0; j < width; j++)
			{
				rotatedIndex = 
					angle == 90 ? (height - i - 1) + j * height : 
					(angle == 270 ? i + height * (width - j - 1) : 
						width * height - (i * width + j) - 1
					);
	 
				rotatedData[rotatedIndex] = rowData[j];
			}
		}
	 
		if(angle == 90 || angle == 270)
		{
			return Image.createRGBImage(rotatedData, height, width, true);
		}
		else
		{
			return Image.createRGBImage(rotatedData, width, height, true);
		}
	}
	
	public static void fillTriangle(Graphics g, int x, int y, int w, int h, int orientation){

		switch( orientation ){
		case HEAD_UP:
			g.fillTriangle(x-w, y+h, x, y, x+w, y+h);
			break;

		case HEAD_DOWN:
			g.fillTriangle(x-w, y-h, x, y, x+w, y-h);			
			break;

		case HEAD_LEFT:
			g.fillTriangle(x+w, y-h, x, y, x+w, y+h);
			break;

		case HEAD_RIGHT:
			g.fillTriangle(x-w, y-h, x, y, x-w, y+h);
			break;
			
		case HEAD_DOWN_RECTANGLE_MODE:
			g.fillTriangle(x, y, x+(w/2), y+(w/2), x+w, y);
			break;
			
		case HEAD_UP_RECTANGLE_MODE:
			g.fillTriangle(x, y+h, x+(w/2), y+(h/2), x+w, y+h);
			break;

		}
	}
	
	public void drawTransparentImage(Graphics g, Image img, int x, int y, int trans){
		
		int width;			
		int height;		
			
		width = img.getWidth();
        height = img.getHeight();        
        
        if( imageData == null ){
        	imageData = new int[width * height];
        }
        
        img.getRGB(imageData, 0, width, 0, 0, width, height);
		
		for (int i = 0; i < imageData.length; i++) {
            int kts = (imageData[i] >> 24) & 0xff;
            int ncolor = (imageData[i] & 0x00FFFFFF);
            kts = trans;
            kts = (kts << 24);
            ncolor += kts;
            imageData[i] = ncolor;
        }
		
		g.drawRGB(imageData, 0, width, x, y, width, height, true);
		
	}
	
	public static final void fillGradientRect(Graphics g, int color1, int color2, int left, int top, int width, int height, int orientation)
	{
		int max = orientation == VERTICAL_GRADIENT ? height : width;
		
		for(int i = 0; i < max; i++)
		{
			
			int prop = max * (max - 1 - i) / (max - 1);
			
			int red = 
				(((color1 >> 16) & 0xff) * prop +
				((color2 >> 16) & 0xff) * (max - prop)) / max;
			
			int green = 
				(((color1 >> 8) & 0xff) * prop +
				((color2 >> 8) & 0xff) * (max - prop)) / max;
			
			int blue = 
				(((color1 >> 0) & 0xff) * prop +
				((color2 >> 0) & 0xff) * (max - prop)) / max;
			
			int color = red << 16 | green << 8 | blue;
			
			g.setColor(color);
			
			if(orientation == VERTICAL_GRADIENT){
				g.drawLine(left, top + i, left + width - 1, top + i);
			}else{
				g.drawLine(left + i, top, left + i, top + height - 1);
			}
		}
	}
	
	public static void fillRoundRectangle(
			Graphics g, int background, int x, int y, 
			int w, int h, int border_color, int round, boolean iluminate
			){
		
		if( iluminate ){
			drawIlumination(g, x, y, w, h);
		}
		
		g.setClip(x, y, w, h);
		g.setColor(background);
		g.fillRoundRect(x+2, y+2, w-2, h-3, round-6, round-6);
				
		g.setColor(border_color);
		g.drawRoundRect(x+1, y+1, w-2, h-3, round-3, round-3);			
		g.setColor(border_color);
		g.drawRoundRect(x, y, w, h-1, round, round);
				
	}
	
	private static void createIlumination()throws Exception{
		if( sombra == null ){
			sombra = Image.createImage("/msombra.png");
			sombraf = Image.createImage("/sombraf.png");
		}
	}
	
	public static void drawIlumination(
			Graphics g, int x, int y, int w, int h
			){
		
		try{
			
			createIlumination();
			
			if( sombra.getWidth() >= w ){
			
				g.setClip(x, y-13, w-sombraf.getWidth(), h*2);		
				g.drawImage(sombra, x-5, y-10, Graphics.LEFT|Graphics.TOP);
			
				g.setClip(x, y-13, w, h*2);
				g.drawImage(sombraf, (x+w-sombraf.getWidth()), y-10, Graphics.LEFT|Graphics.TOP);
				
				g.setClip(x, y, w-sombraf.getWidth(), h*2);
				g.drawImage(sombra, x-5, (y+h)-18, Graphics.LEFT|Graphics.TOP);
				
				g.setClip(x, y-13, w, h*2);
				g.drawImage(sombraf, (x+w-sombraf.getWidth()), (y+h)-18, Graphics.LEFT|Graphics.TOP);
			
			}
			
		}catch (Exception e) {
			e.printStackTrace();
		}
		
	}

}
